<?php if ( !defined('BASEPATH')) exit('No direct script access allowed');

class Charge_elements extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');

	}
	public function index(){
	  $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  $this->load->view($this->session->userdata('formain'),array(
	  	  'engine'			=> $this->charge_elements_model->get_engine()
	  	));
	  $this->load->view('layouts/footer');
	 }

	 public function insert_charge_element(){
	 	$data = array(
	 		':p_charge_code'		=> $this->input->post('chg_code'),
	 		':p_charge_name'		=> $this->input->post('chg_name'),
	 		':p_description'		=> $this->input->post('chg_desc'),
	 		':p_compute_engine'		=> $this->input->post('chg_eng'),
	 		':p_proc_name'			=> '',
	 		':p_user_id'			=> $this->session->userdata('user_id')
	 		);
	 	$this->charge_elements_model->insert_chg_element($data);
	 }
	 public function update_charge_element(){
	 	$data = array(
	 		':p_charge_name'		=> $this->input->post('e_chg_name'),
	 		':p_description'		=> $this->input->post('e_chg_desc'),
	 		':p_compute_engine'		=> $this->input->post('e_chg_eng'),
	 		':p_charge_id'			=> $this->input->post('e_chg_id'),
	 		':p_user_id'			=> $this->session->userdata('user_id')
	 		);
	 	$this->charge_elements_model->update_chg_element($data);
	 }
	 public function update_chg_option(){
	 	$ena = $this->input->post('m_opt_ena');
	 	$data = array(
	 			':p_option_value'	=> $this->input->post('opt_value'),
	 			':p_enable_flag'	=> ($ena == "ok" ? 'Y' : 'N'),	
	 			':p_option_code'	=> $this->input->post('opt_edit_code'),
	 			':p_user_id'		=> $this->session->userdata('user_id'),
	 			':p_option_name'	=> $this->input->post('menu_name_edit')
	 		);
	 	$this->charge_elements_model->update_chg_option($data);
	 }
	 public function insert_chg_option(){
	 	$ena = $this->input->post('opt_ena');
	 	$chg_id = $this->input->post('chg_id');
		$data = array(
			':p_charge_id'			=> $chg_id,
			':p_option_code'		=> $this->input->post('opt_code'),
			':p_option_name'		=> $this->input->post('opt_name'),
			':p_option_value'		=> $this->input->post('opt_val'),
			':p_enable_flag'		=> ($ena == "ok" ? 'Y' : 'N'),
			':p_user_id'			=> $this->session->userdata('user_id')
			);



		$query = $this->charge_elements_model->insert_chg_option($data);
		if($query[0]->error == 1){
			echo json_encode($query);
		}else{
		$this->view_options($chg_id);
		}
	 }
	public function view_options($id){
		$query = $this->charge_elements_model->get_option_chg($id);
		echo json_encode($query);
	}

	public function search_chg_element(){
		$code 		= $this->input->post('s_chg_code');
		$name 	 	= $this->input->post('s_chg_name');
		$eng 		= $this->input->post('s_chg_eng');
		//var_dump($code);
		$query = $this->charge_elements_model->search_chg_element($code,$name,$eng);

		if(!empty($query)) {
			echo json_encode($query);
		}else{
			echo json_encode("none");
		}
	}





	public function add_form(){
		$this->load->view($this->session->userdata('foradd'),array(
	  	  'engine'			=> $this->charge_elements_model->get_engine()
	  	));
	}
	public function edit_form($id){
		$this->load->view($this->session->userdata('foredit'),array(
	  	  'engine'			=> $this->charge_elements_model->get_engine(),
	  	  'data'			=> $this->charge_elements_model->get_chg_element($id),
	  	  'opt_data'		=> $this->charge_elements_model->get_option_chg($id)
	  	));
	}
	public function view_form($id){
		$this->load->view($this->session->userdata('forview'),array(
	  	  'engine'			=> $this->charge_elements_model->get_engine(),
	  	  'data'			=> $this->charge_elements_model->get_chg_element($id),
	  	  'opt_data'		=> $this->charge_elements_model->get_option_chg($id)
	  	));
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */